package com.wilmer.tw.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wilmer.common.annotation.Anonymous;
import com.wilmer.vo.LineVo;
import com.wilmer.tw.domain.ChildTw;
import com.wilmer.vo.TwVo;
import org.apache.commons.compress.utils.Lists;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wilmer.common.annotation.Log;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.tw.domain.ChildTw;
import com.wilmer.tw.service.IChildTwService;
import com.wilmer.common.utils.poi.ExcelUtil;
import com.wilmer.common.core.page.TableDataInfo;

/**
 * 孩子体温Controller
 * 
 * @author ruoyi
 * @date 2024-04-26
 */
@RestController
@RequestMapping("/tw/tw")
public class ChildTwController extends BaseController
{
    @Autowired
    private IChildTwService childTwService;

    /**
     * 查询孩子体温列表
     */
    @PreAuthorize("@ss.hasPermi('tw:tw:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChildTw childTw)
    {
        startPage();
        List<ChildTw> list = childTwService.selectChildTwList(childTw);
        return getDataTable(list);
    }

    /**
     * 导出孩子体温列表
     */
    @PreAuthorize("@ss.hasPermi('tw:tw:export')")
    @Log(title = "孩子体温", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChildTw childTw)
    {
        List<ChildTw> list = childTwService.selectChildTwList(childTw);
        ExcelUtil<ChildTw> util = new ExcelUtil<ChildTw>(ChildTw.class);
        util.exportExcel(response, list, "孩子体温数据");
    }

    /**
     * 获取孩子体温详细信息
     */
    @PreAuthorize("@ss.hasPermi('tw:tw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(childTwService.selectChildTwById(id));
    }

    /**
     * 新增孩子体温
     */
    @PreAuthorize("@ss.hasPermi('tw:tw:add')")
    @Log(title = "孩子体温", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChildTw childTw)
    {
        return toAjax(childTwService.insertChildTw(childTw));
    }

    /**
     * 修改孩子体温
     */
    @PreAuthorize("@ss.hasPermi('tw:tw:edit')")
    @Log(title = "孩子体温", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChildTw childTw)
    {
        return toAjax(childTwService.updateChildTw(childTw));
    }

    /**
     * 删除孩子体温
     */
    @PreAuthorize("@ss.hasPermi('tw:tw:remove')")
    @Log(title = "孩子体温", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(childTwService.deleteChildTwByIds(ids));
    }

    /**
     * 查询每天孩子的体温
     * @return
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('tw:tw:daily')")
    @GetMapping("/temperature")
    public TwVo twChartsDaily(@Param("userId") Long userId, @Param("days") Long days)
    {
        //获取体温集合
        List<ChildTw> twList = childTwService.twChartsDaily(userId,days);

        //初始化Vo对象
        TwVo twVo = new TwVo();
        twVo.setXdata(Lists.newArrayList());
        twVo.setSeries(Lists.newArrayList());

        twList.forEach( tw->{
            twVo.getXdata().add( formatDate(tw.getDatetime()));
            twVo.getSeries().add(tw.getTw());
        });

        return twVo;
    }

    /**
     * 根据父母id查询孩子的体温列表
     * @param userId
     * @return
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('ys:ys:child')")
    @GetMapping("/child")
    public TableDataInfo getListByUserId(@Param("userId") Long userId){
        startPage();
        List<ChildTw> list = childTwService.getListByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 日期转换
     * @param date
     * @return
     */
    private String formatDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }



}
